package com.wunu.smartlock1.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wunu.smartlock1.MainActivity;
import com.wunu.smartlock1.R;
import com.wunu.smartlock1.net.NetComponent;
import com.wunu.smartlock1.net.api.LockEventApi;
import com.wunu.smartlock1.net.param.PA;
import com.wunu.smartlock1.net.param.PB;
import com.wunu.smartlock1.net.response.LoginResult;
import com.wunu.smartlock1.net.response.URegistResponse;
import com.wunu.smartlock1.net.response.USendCaptcha;
import com.wunu.smartlock1.service.AccountService;

import retrofit2.Call;


/**
 * 登录界面
 * Created by admin on 2018/4/21.
 */

public class LoginActivityX extends AppCompatActivity{

    Button login;
    Button login2;
    EditText a;
    EditText a_1;
    TextView t;
    TextView t_b;
    Intent intent = new Intent();
    String phone;
    EditText e;
    EditText e2;
    EditText e3;
    String captcha;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            switch (msg.what) {
                case 1:
                    USendCaptcha rs = (USendCaptcha) msg.obj;
                    System.out.println("msg----------------------->:" + rs);
                    if (rs == null || rs.getCode() != 200) {
                        Toast.makeText(LoginActivityX.this, rs.getMsg(), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    //只要执行到这里就关闭对话框
                    Toast.makeText(LoginActivityX.this, "验证码发送成功，请注意查收", Toast.LENGTH_SHORT).show();
                    captcha = rs.getCaptcha();
                    break;
                case 2:
                    URegistResponse rs2 = (URegistResponse)msg.obj;
                    System.out.println("msg----------------------->:" + rs2);
                    if (rs2 == null || rs2.getCode() != 200) {
                        Toast.makeText(LoginActivityX.this, rs2.getMsg(), Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Toast.makeText(LoginActivityX.this,"注册成功!", Toast.LENGTH_SHORT).show();
                    startActivity(intent.setClass(LoginActivityX.this, LoginActivityA.class));
            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartlock_lock_login_v);
        login = findViewById(R.id.btn_login2);
        e = findViewById(R.id.et_account);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone = e.getText().toString();
                System.out.println("phnoe: "+phone);
                if(phone == null || phone.length()<11){
                    Toast.makeText(LoginActivityX.this, "请输入手机号码!", Toast.LENGTH_SHORT).show();
                    return;
                }
                new Thread(){
                    public void run(){
                        try {
                            LockEventApi service = NetComponent.UserRetrofit.create(LockEventApi.class);
                            PA pa = new PA();
                            pa.setAccount(phone);
                            pa.setRegister(true);
                            Call<USendCaptcha> call  = service.sendCaptchaA(pa);
                            USendCaptcha result = call.execute().body();
                            System.out.println(result);
                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            msg.obj = result;
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
        e2 = findViewById(R.id.ccz);
        e3 = findViewById(R.id.cc);
        login2 = findViewById(R.id.btn_login);
        login2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("captcha: "+captcha);
                if(captcha == null || !captcha.equals(e2.getText().toString())){
                    Toast.makeText(LoginActivityX.this, "请确认验证码是否正确", Toast.LENGTH_SHORT).show();
                    return;
                }
                final String pd = e3.getText().toString();
                new Thread(){
                    public void run() {
                        try {
                            LockEventApi service = NetComponent.UserRetrofit.create(LockEventApi.class);
                            PB pb = new PB();
                            pb.setAccount(phone);
                            pb.setPassword(pd);
                            System.out.println("注册: "+pb);
                            Call<URegistResponse> call = service.registA(pb);
                            URegistResponse result = call.execute().body();
                            System.out.println(result);
                            // 发送消息，把服务器返回的本文弹出吐司显示
                            Message msg = handler.obtainMessage();
                            msg.obj = result;
                            msg.what = 2;
                            handler.sendMessage(msg);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }


}
